//
//  BranchStoreViewController.m
//  OrderMeal
//
//  Created by 宏创 on 14-3-6.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "BranchStoreViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "BranchStoreCell.h"
#import "MenuListViewController.h"
#import "MBProgressHUD.h"
@interface BranchStoreViewController ()
{
    NSArray *_dataArray;   //数据
    NSDictionary *_dict;  //数据字典
    NSString *_regionID;   //店铺ID
    NSString *_storeName;  //店铺名字
    NSArray *_buildingArray;
    MBProgressHUD *hud;
}

@end

@implementation BranchStoreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:view];
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"加载数据中";
    [hud show:YES];
    //[self.collectionView registerClass:[BranchStoreCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    
//    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
//    [layout setItemSize:CGSizeMake(100, 150)];
//    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
//    [self.collectionView setCollectionViewLayout:layout];
   
    [self loadData];
}

-(void)loadData
{
    AFHTTPRequestOperationManager *manger=[AFHTTPRequestOperationManager manager];
    [manger GET:region_url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSNumber *status=responseObject[@"success"];
        if ([status isEqualToNumber:@1]) {
            _dataArray= responseObject[@"retData"][@"regions"];
            [self.tableView reloadData];
            [hud removeFromSuperview];
        }else{
            NSLog(@"---%@",responseObject[@"msg"]);
        }

      
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"---无法访问");
    }];
}

#pragma mark -data
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"regions"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"regions"];
    }
    cell.imageView.image = [UIImage imageNamed:@"collection.jpg"];
    cell.imageView.frame = CGRectMake(0, 0, 40, cell.frame.size.height);
    NSInteger row = [indexPath row];
    cell.textLabel.text = _dataArray[row][@"name"];
    return cell;
}


#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    _dict= _dataArray[indexPath.row];
    _regionID=[_dict objectForKey:@"id"];
    _buildingArray=[_dict objectForKey:@"building"];
    
    MenuListViewController *menuList=segue.destinationViewController;
    menuList.regionID=_regionID;
    menuList.buildingArray=_buildingArray;
}

@end
